package com.changgou.order.service.impl;

import com.changgou.entity.Result;
import com.changgou.goods.feign.SkuFeign;
import com.changgou.goods.feign.SpuFeign;
import com.changgou.goods.pojo.Sku;
import com.changgou.goods.pojo.Spu;
import com.changgou.order.pojo.OrderItem;
import com.changgou.order.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CartServiceImpl implements CartService {
  private static final String CART = "Cart_";
  @Autowired
  private RedisTemplate redisTemplate;//redis模板类
  @Autowired
  private SkuFeign skuFeign;
  @Autowired
  private SpuFeign spuFeign;

  /**
   * @param skuId    商品的skuid
   * @param num      商品的数量
   * @param username 用户名称:区分是谁的购物车
   */
  @Override
  public void addCart(String skuId, Integer num, String username) {
    //1. 购物车信息都是直接保存在redis中的,我们直接从redis查.在redis中以hash类型保存的
    OrderItem orderItem = (OrderItem) redisTemplate.boundHashOps(CART + username).get(skuId);
    //2. redis存在,则更新商品的数量和价格做个更新
    if (orderItem != null) {
      orderItem.setNum(orderItem.getNum() + num); //原有的数量+这次传递的数量
      orderItem.setMoney(orderItem.getNum() * orderItem.getPrice());
      orderItem.setPayMoney(orderItem.getNum() + orderItem.getPrice());
    } else {
      //3. redis中不存在,则通过skuid添加商品
      Result<Sku> skuResult = skuFeign.findById(skuId);
      Sku sku = skuResult.getData();//得到sku
      Result<Spu> spuResult = spuFeign.findById(sku.getSpuId());
      Spu spu = spuResult.getData();
      //sku和spu都封装orderItem
      orderItem = this.sku2OrderItem(sku, spu, num);
    }
//    4. 把orderItem重新添加到购物车缓存
    redisTemplate.boundHashOps(CART + username).put(skuId, orderItem);
  }


  /**
   * 查询购物车
   *
   * @param username 用户名称
   *                 定义返回类型为Map,因为返回信息不仅有商品还有商品数量和价格
   */
  @Override
  public Map list(String username) {
    Map<Object, Object> map = new HashMap<>();
    List<OrderItem> orderItemList = (List<OrderItem>) redisTemplate.boundHashOps(CART + username).values();
    map.put("orderItemList", orderItemList);  //购物车商品
    //商品数量与总价格
    Integer totalNum = 0;  //商品数量
    Integer totalPrice = 0;  //商品总价格
    for (OrderItem orderItem : orderItemList) {
      totalNum += orderItem.getNum();  //遍历集合获取最新商品数量
      totalPrice += orderItem.getPrice();
    }
    map.put("totalNum", totalNum);
    map.put("totalPrice", totalPrice);

    return null;
  }

  //sku和spu都封装orderItem
  private OrderItem sku2OrderItem(Sku sku, Spu spu, Integer num) {
    OrderItem orderItem = new OrderItem();
    orderItem.setSpuId(sku.getSpuId());
    orderItem.setSkuId(sku.getId());
    orderItem.setName(sku.getName());
    orderItem.setPrice(sku.getPrice());
    orderItem.setNum(num);
    orderItem.setMoney(num * orderItem.getPrice()); //单价*数量
    orderItem.setPayMoney(num * orderItem.getPrice()); //实付金额
    orderItem.setImage(sku.getImage());
    orderItem.setWeight(sku.getWeight() * num); //重量=单个重量*数量
    //分类ID设置
    orderItem.setCategoryId1(spu.getCategory1Id());
    orderItem.setCategoryId2(spu.getCategory2Id());
    orderItem.setCategoryId3(spu.getCategory3Id());
    return orderItem;
  }
}
